//
//  UITextView+ToolBar.m
//  Runone
//
//  Created by runone01 on 16/10/22.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "UITextView+ToolBar.h"

@implementation UITextView (ToolBar)

- (void)setTextFieldInputAccessoryView:(UITextView *)TF frame:(CGRect)rect{
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:rect];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * spaceBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(2, 5, 40, 25);
    [rightBtn addTarget:self action:@selector(dealKeyboardHide) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *doneBtnItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    UIButton *leftbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [leftbtn setTitle:@"取消" forState:UIControlStateNormal];
    leftbtn.frame = CGRectMake(2, 5, 40, 25);
    [leftbtn addTarget:self action:@selector(dealKeyboardHide) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftitem = [[UIBarButtonItem alloc]initWithCustomView:leftbtn];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:leftitem,spaceBtn,doneBtnItem,nil];
    [topView setItems:buttonsArray];
    
    [TF setInputAccessoryView:topView];
    [TF setAutocorrectionType:UITextAutocorrectionTypeNo];
    [TF setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    
}

- (void)dealKeyboardHide {
    [UIView animateWithDuration:1 animations:^{
        [self resignFirstResponder];
    }];
    
    //    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

@end
